const express = require('express')
const AuthController = require('../controllers/authController')
const { validateUserRegister, validateUserLogin, validateRefreshToken, validateUserChangePassword, validatePasswordResetRequest, validatePasswordReset, validateCheckPermission } = require('../middleware/validation')
const { authenticateToken, authenticateRefreshToken } = require('../middleware/auth')
const { authLimiter } = require('../middleware/security')

const router = express.Router()

// 用户注册
router.post('/register', 
  authLimiter,
  validateUserRegister,
  AuthController.register
)

// 用户登录
router.post('/login',
  authLimiter,
  validateUserLogin,
  AuthController.login
)

// 刷新访问令牌
router.post('/refresh',
  authLimiter,
  validateRefreshToken,
  authenticateRefreshToken,
  AuthController.refreshToken
)

// 用户登出
router.post('/logout',
  authenticateToken,
  AuthController.logout
)

// 修改密码
router.put('/password',
  authenticateToken,
  validateUserChangePassword,
  AuthController.changePassword
)

// 请求密码重置
router.post('/password/reset-request',
  authLimiter,
  validatePasswordResetRequest,
  AuthController.requestPasswordReset
)

// 重置密码
router.post('/password/reset',
  authLimiter,
  validatePasswordReset,
  AuthController.resetPassword
)

// 获取当前用户信息
router.get('/me',
  authenticateToken,
  AuthController.getCurrentUser
)

// 验证令牌
router.post('/verify',
  authenticateToken,
  AuthController.verifyToken
)

// 获取用户权限
router.get('/permissions',
  authenticateToken,
  AuthController.getUserPermissions
)

// 检查特定权限
router.post('/permissions/check',
  authenticateToken,
  validateCheckPermission,
  AuthController.checkPermission
)

module.exports = router